import { observable, action, computed } from "mobx";
import { WeaTableNew, WeaForm } from 'comsMobx'
import { each } from 'lodash';
import { message,Modal } from "antd";
import * as Apis from '../../apis/monitor';
import * as Action from './action';
const { TableStore } = WeaTableNew;
import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;
export default class TaskMonitorStore {

    
    @observable leftDatas = [];
    @observable selectedApp = '';
    @observable searchValue = '';

    @observable tableStore = new TableStore();
    @observable rightMenus = [];

    @observable loading = true;
    @observable isRight = false;

    @observable conditionVisible = false;
    @observable conditionForm = new WeaForm();
    conditions;
    constructor() {
        each(Action, (value, key) => {
            this[key] = action(value.bind(this));
        });
        this.conditions = this.getConditions();//通过上面的操作 this可以拿到action.js中定义的方法???
        this.conditionForm.initFormFields(this.conditions);//from组件调用方法初始化
    }

    @action
    init = () => {
        this.initTable();
        this.getAllTaskTypeList();
    }

   
    @action
    getAllTaskTypeList = () => {
        Apis.getTaskTypeList({by:'taskMonitor'}).then(result => {
            this.leftDatas = result.data;
        });
    }

    @action
    initTable = () => {
        Apis.getTaskList({  
            appid: this.selectedApp, 
            type: 'my',
            by:'taskMonitor',
            quickSearchValue: this.quickSearchValue,
            ...this.conditionForm.getFormParams()//取from组件中所有参数传入接口
        }).then(result => {
            if (result.api_status == true) {
                this.loading = false;
                this.isRight = result.data.isRight;
                if(!this.isRight) return;
                this.tableStore.getDatas(result.data.datas, 1);
                this.rightMenus = result.data.datas.rightMenus;
            } else {
                message.error(result.api_errormsg);
            }
        });
    }

    @computed get apps() {
        return this.leftDatas.filter(d => d.name.toLowerCase().indexOf(this.searchValue.toLowerCase()) > -1);
    }
    @computed get quickSearchValue(){
        if (this.conditionForm.isFormInit && this.conditionForm.has('taskName')) {
            return this.conditionForm.$('taskName').values();
        } 
        return '';
    }
    @action
    changeSelectedApp = (selectedApp) => {
        this.changeQuickSearchValue('');
        this.selectedApp = selectedApp;
        this.initTable();
    }
    @action
    setLeftSearchValue = (value) => {
        this.searchValue = value;
    }

    @action
    reloadTable = () => {
        this.tableStore.reloadCurrent();
        this.init();
    }

    @action deleteTask = () => {
        const _this = this;
        const { selectedRowKeys } = this.tableStore;
        if (selectedRowKeys.length > 0) {
            Modal.confirm({
                content: getLabel('505984', '是否确认删除？'),
                onOk() {
                    Apis.deleteTask({
                        ids: selectedRowKeys.slice().join(',')
                    }).then(result => {
                        _this.reloadTable();
                        message.success(getLabel('505985', '删除成功'));
                    });
                },
                onCancel() { },
            });
        } else {
            message.error(getLabel('507492', '请选择至少一条记录!'));
        }
    }
}